package com.netcracker.util.service;

import com.netcracker.db.exception.DBException;
import com.netcracker.entity.Author;
import com.netcracker.entity.record.AuthorRecord;
import com.netcracker.util.dao.DAOFacade;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import com.netcracker.util.service.exception.WrongAuthorException;
import java.util.List;


public class AuthorService {

    private DAOFacade daoFacade;
    
    public AuthorService(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }
    
    public void setDAOFacade(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }

    public void addAuthor(AuthorRecord authorRecord) throws WrongAuthorException, DataTransmittingException {
       checkData(authorRecord);
        try {
            daoFacade.getAuthorDAO().create(authorRecord);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
    }

    public void deleteAuthor(int id) throws DataTransmittingException {
        try {
            daoFacade.getAuthorDAO().remove(id);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
    }

    public Author findAuthorById(int id) throws DataTransmittingException, NullDataException {
        Author author = null;
        try {
            author = daoFacade.getAuthorDAO().find(id);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
        if (author==null)
        {
            throw new NullDataException("Author with this id doesn't exist");
        }
        return author;
    }

    public List<Author> getAllAuthors() throws DataTransmittingException, NullDataException {
        List<Author> authors = null;
        try {
            authors = daoFacade.getAuthorDAO().findAll();
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
        if (authors==null)
        {
            throw new NullDataException("No one author was found");
        }
        return authors;
    }

    public List<Author> findAuthorsByName(String name) throws DataTransmittingException, NullDataException {
        List<Author> authors = null;
        try {
            authors = daoFacade.getAuthorDAO().findAuthorByName(name);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
        if (authors==null)
        {
            throw new NullDataException("No one author was found");
        }
        return authors;
    }

    public void updateAuthor(AuthorRecord authorRecord) throws WrongAuthorException, DataTransmittingException {
        checkData(authorRecord);
        try {
            daoFacade.getAuthorDAO().edit(authorRecord);
        } catch (DBException e) {
            throw new DataTransmittingException(e.getMessage(), e);
        }
    }

    private void checkData(AuthorRecord authorRecord) throws WrongAuthorException {
        if (authorRecord.getFullName() == null || authorRecord.getFullName().equals(""))
        {
            throw new WrongAuthorException("Not all fields of author are filled correct!");
        }
    }
    
}
